<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <log-search @search="reload" />
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
          >
            删除
          </el-button>

          <el-button
            size="small"
            type="warning"
            class="ele-btn-icon"
            @click="refundBatch"
          >
            退款
          </el-button>
        </template>

        <template v-slot:user="{ row }">
          <div class="ele-cell" v-if="row.figureAccount">
            <div class="ele-cell-content">
              <div class="ele-cell-title"
                >{{ row.figureAccount.telphone }}
              </div>
            </div>
          </div>
          <span v-else>无</span>
        </template>

        <template v-slot:title="{ row }">
          <el-tag
            v-if="row.type == 1"
            size="small"
            effect="light"
            type="primary"
            class="ele-tag-round"
            >高</el-tag
          >
          <el-tag
            v-if="row.type == 2"
            size="small"
            effect="light"
            type="success"
            class="ele-tag-round"
            >极</el-tag
          >
          <span>{{ row.title }}</span>
          <div v-if="row.way == 4">{{ row.wo_name }}</div>
        </template>

        <template v-slot:current_status="{ row }">
          <el-tag v-if="row.current_status == 'success'" type="success"
            >生成成功</el-tag
          >

          <el-tag
            v-else-if="
              row.current_status == 'abnormal' &&
              row._id == '' &&
              row.is_refund == 1
            "
            type="warning"
            >异常失败,已退款</el-tag
          >

          <el-tag
            v-else-if="
              row.current_status == 'abnormal' &&
              row._id == '' &&
              row.is_refund != 1
            "
            type="info、"
            >异常失败,未退款</el-tag
          >

          <el-tag
            v-else-if="
              row.current_status == '' ||
              row.current_status == 'copy' ||
              row.current_status == 'init' ||
              row.current_status == 'start' ||
              row.current_status == 'pending' ||
              row.current_status == 'process'
            "
            >生成中</el-tag
          >
        </template>

        <template v-slot:result="{ row }">
          <!-- <video width="320" height="240" controls :src="row.result"  v-if="row.result"> </video> -->
          <!-- <span v-else>--</span> -->
          <div class="img-all">
            <div v-if="row.result">
              <el-image
                fit="contain"
                :src="row.result_cover"
                class="img-video"
                v-if="row.result_cover"
              ></el-image>

              <div class="d-icon flex pointer" v-if="row.result_cover">
                <i
                  class="el-icon-caret-right icon-default"
                  @click="lookVideo(row.result)"
                ></i>
              </div>
              <video
                width="320"
                height="240"
                controls
                :src="row.result"
                v-if="!row.result_cover"
              >
              </video>
            </div>
            <span
              style="
                display: flex;
                justify-content: center;
                align-items: center;
                width: 100%;
                height: 100%;
              "
              v-else
              >--</span
            >
          </div>
        </template>

        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-popconfirm
            v-if="row.current_status == 'abnormal' && row.is_refund != 1 &&row.composite_account_id <= 1"
            class="ele-action"
            title="确定要给用户退款吗？"
            @confirm="refund(row)"
          >
            <template v-slot:reference>
              <el-link type="red" :underline="false"> 退款 </el-link>
            </template>
          </el-popconfirm>

         <el-popconfirm
            class="ele-action"
            title="确定要删除此数据吗？"
            @confirm="remove(row)"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>

          <el-link
            type="success"
            :underline="false"
            icon="el-icon-more"
            @click="openMore(row)"
          >
            更多
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <check :visible.sync="showCheck" :data="current" @done="reload" />

    <more :visible.sync="showMore" :data="currentMore" @done="reload" />
    <look-video
      :visible.sync="videoVisible"
      :videoUrl="videoUrl"
      ref="videoRef"
    >
    </look-video>
  </div>
</template>

<script>
import LogSearch from './components/log-search';
import Check from './components/check';
import More from './components/more';
import LookVideo from './components/look-video';
import {
  errorVideo,
  remove,
  modify,
  resubmit,
  changeStatus,
  refund
} from '@/api/video';

export default {
  name: 'Video',
  components: {
    LogSearch,
    Check,
    LookVideo,
    More
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: 'left'
        },
        {
          prop: 'user',
          label: '开发者',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'user'
        },
        {
          prop: 'title',
          label: '名称',
          showOverflowTooltip: true,
          minWidth: 100,
          slot: 'title'
        },
        {
          prop: 'error_msg',
          label: '异常信息',
          showOverflowTooltip: true,
          minWidth: 100
        },

        {
          prop: 'current_status',
          label: '状态',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 90,
          slot: 'current_status'
        },

        {
          prop: 'create_time',
          label: '创建时间',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 90
        },

        {
          columnKey: 'action',
          label: '操作',
          width: 300,
          align: 'center',
          resizable: false,
          slot: 'action',
          hideInSetting: true,
          showOverflowTooltip: true
        }
      ],
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      currentMore: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示审核弹窗
      showCheck: false,
      //弹框名称
      id: 0,
      idMore: null,
      videoVisible: false,
      showMore: false,
      videoUrl: ''
    };
  },
  methods: {
    lookVideo(item) {
      console.log('预览视频iiii', item);
      this.videoUrl = item;
      this.videoVisible = true;
      this.$nextTick(() => {
        this.$refs['videoRef'].show();
      });
    },

    /* 表格数据源 */
    datasource({ page, limit, where, order }) {
      return errorVideo({ ...where, ...order, page, limit });
    },
    /* 刷新表格 */
    reload(where) {
      this.$refs.table.reload({ page: 1, where: where });
    },
    /* 打开编辑弹窗 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 打开编辑弹窗 */
    openCheck(row) {
      this.id = row.id;
      this.current = row;
      this.showCheck = true;
    },

    /*  */
    openMore(row) {
      this.idMore = row.id;
      this.currentMore = row;
      this.showMore = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      remove(row.id)
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },

    refund(row) {
      const loading = this.$loading({ lock: true });
      refund(row.id)
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 失败回调 */
    changeStatus(row) {
      const loading = this.$loading({ lock: true });
      changeStatus({ id: row.id })
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },

    /* 重新提交 */
    resubmit(row) {
      const loading = this.$loading({ lock: true });
      resubmit({ id: row.id })
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },

    /* 退款 */

    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的数据吗?', '提示', {
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          remove(this.selection.map((d) => d.id))
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },
    refundBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要退款选中的数据吗?', '提示', {
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          refund(this.selection.map((d) => d.id))
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },

    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({ lock: true });
      modify({ id: row.id, field: 'is_display', value: row.is_display })
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
        })
        .catch((e) => {
          loading.close();
          row.status = !row.status ? 1 : 0;
          this.$message.error(e.message);
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.img-all {
  position: relative;
  height: 210px;
  border-radius: 10px;
}
/*审核失败*/
.d-fail-img {
  background-color: rgba(25, 26, 35, 0.61);
  border-radius: 10px;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 210px;
}

.img-none {
  background-color: #f3f3f3;
  width: 100%;
  height: 210px;
  border-radius: 10px;
}
.img-video {
  background-color: #000;
  width: 100%;
  height: 210px;
  border-radius: 10px;
}

.d-icon {
  position: absolute;
  top: 0%;
  left: 0%;
  width: 100%;

  height: 210px;
}

.icon-default {
  background-color: rgba(10, 0, 167, 0.09);
  border-radius: 50% !important;
  z-index: 99;
  display: none;
  color: #ffffff;
  font-size: 40px;
  width: 50px;
  height: 50px;
  position: absolute;
  top: 40%;
  left: 50%;
  transform: translateX(-50%);

  // transform: translateY(-50%);
  box-shadow: 1px 4px 4px 1px #b1b1b1;
}

.d-icon:hover {
  .icon-default {
    display: block;
  }
}

.drag {
  background: #000 !important;
  background-image: linear-gradient(#333, #999) !important;
}

.ele-cell-content {
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.el-icon-caret-right:before {
  position: relative;
  top: 14%;
}
</style>
